#include"PlaylistView.h" 

#include"ListWidget.h"
#include"Playlist.h"
#include"SongItem.h"
#include"Song.h"

PlaylistView::PlaylistView(QWidget *parent):QWidget(parent){
	m_playlist = 0;
	m_current = 0;
	m_actionPlayPause = 0;
	
	m_layout = new QVBoxLayout(this);
	setLayout(m_layout);
	
	m_listWidget = new ListWidget(this);
	m_layout->addWidget(m_listWidget);
	connect(m_listWidget, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(slotItemActivated(QListWidgetItem*)));
	connect(m_listWidget, SIGNAL(addSong(QPointer<Song>, short int)), this, SLOT(slotAddSongToPlaylist(QPointer<Song>, short int)));
	connect(m_listWidget, SIGNAL(requestedEditMetadata(QList<QString>)), this, SLOT(slotRequestedEditMetadata(QList<QString>)));
	connect(m_listWidget, SIGNAL(removeSongs(QList<short int>)), this, SLOT(slotRemoveSongs(QList<short int>)));
	
	m_buttonWidget = new QWidget(this);
	m_layout->addWidget(m_buttonWidget);
	
	m_buttonLayout = new QHBoxLayout(m_buttonWidget);
	m_buttonWidget->setLayout(m_buttonLayout);
	
	m_buttonClear = new QPushButton( QIcon::fromTheme("edit-clear-list"), "Clear", m_buttonWidget );
	m_buttonLayout->addWidget(m_buttonClear);
	connect(m_buttonClear, SIGNAL(clicked()), this, SLOT(slotButtonClear()));
	
	m_playlistMenu = new QMenu("Playlists", m_buttonWidget);
	QAction *action = m_playlistMenu->addAction("Sequential");
	connect(action, SIGNAL(triggered()), this, SLOT(slotPlaylistSequential()));
	action = m_playlistMenu->addAction("Random");
	connect(action, SIGNAL(triggered()), this, SLOT(slotPlaylistRandom()));
	
	m_playlistMenu->addSeparator();
	action = m_playlistMenu->addAction("No Repeat");
	connect(action, SIGNAL(triggered()), this, SLOT(slotPlaylistNoRepeat()));
	
	action = m_playlistMenu->addAction("Repeat All");
	connect(action, SIGNAL(triggered()), this, SLOT(slotPlaylistRepeatAll()));
	
	action = m_playlistMenu->addAction("Repeat One");
	connect(action, SIGNAL(triggered()), this, SLOT(slotPlaylistRepeatOne()));
	
	
	m_playlistMenuButton = new QToolButton(m_buttonWidget);
	m_playlistMenuButton->setMenu(m_playlistMenu);
	m_playlistMenuButton->setIcon(QIcon::fromTheme("view-media-playlist"));
	m_playlistMenuButton->setCheckable(true);
	m_playlistMenuButton->setPopupMode(QToolButton::InstantPopup);
	m_buttonLayout->addWidget(m_playlistMenuButton);
}

void PlaylistView::addPlayAction(QAction *playPauseAction){
	m_actionPlayPause = playPauseAction;
	connect(this, SIGNAL(play()), playPauseAction, SLOT(trigger()));
}

void PlaylistView::addStopAction(QAction *stopAction){
	connect(this, SIGNAL(stop()), stopAction, SLOT(trigger()));
}

PlaylistView::~PlaylistView(){
	m_playlist = 0;//Deleted by Player
	m_actionPlayPause = 0;
}

void PlaylistView::setPlaylist(Playlist *playlist){
	m_playlist = playlist;
	connect(m_playlist, SIGNAL(songAdded(QPointer<Song>, short int)), this, SLOT(addSong(QPointer<Song>,short int)));
	connect(m_playlist, SIGNAL(currentChanged(short int)), this, SLOT(currentChanged(short int)));
	connect(m_playlist, SIGNAL(cleared()), this, SLOT(slotCleared()));
	for(unsigned i = 0; i < m_playlist->numSongs(); i++)
		addSong(m_playlist->song(i), -1);
}

void PlaylistView::addSong(QPointer<Song> song, short int index){
	//if index == -1, add to back.
	SongItem *s = new SongItem(song, m_listWidget);
	m_listWidget->insertItem(index, s);
}

void PlaylistView::slotAddSongToPlaylist(QPointer<Song> song, short int index){
	emit addSongToPlaylist(song, index);
}

void PlaylistView::currentChanged(short int index){
	if(m_current)
		m_current->highlight(false);
	m_current = 0;
	
	m_current = (SongItem*) m_listWidget->item(index);
	if(m_current)
		m_current->highlight(true);
	else
		throw 1;
}

void PlaylistView::stopAfterCurrent(){
	if(m_current)
		m_current->highlight(true, true);
}

void PlaylistView::slotItemActivated(QListWidgetItem *item){
	//Kinda hacky, but will ensure emitting play() won't pause us.
	if(m_actionPlayPause->text() == "Pause")
		emit stop();
	m_playlist->setIndex( m_listWidget->row(item) );
	emit play();
}

void PlaylistView::slotCleared(){
	m_current = 0;
	while(QListWidgetItem *item = m_listWidget->takeItem(0))
		delete item;
}

void PlaylistView::slotSongRemoved(short int index){
	QListWidgetItem *item = m_listWidget->takeItem(index);
	if( item == m_current )
		m_current = 0;
	delete item;
	if(index == 0 && m_listWidget->count() > 0){
		((SongItem*)m_listWidget->item(index))->setData(2);
	}else if(index > 0 && index < m_listWidget->count()){
		SongItem *current = (SongItem*) m_listWidget->item(index);
		SongItem *previous = (SongItem*) m_listWidget->item(index-1);
		
		if(current->song() == 0 || previous->song() == 0)
			return;
		
		if(current->song()->album() != previous->song()->album()){
			if(current->song()->artist() == previous->song()->artist())
				current->setData(1);
			else
				current->setData(2);
		}
	}
}

void PlaylistView::slotSongDeleted(){
	Song* song = (Song*) sender();
	for(int i = 0; i < m_listWidget->count(); i++){
		SongItem *item = (SongItem*) m_listWidget->item(i);
		if(item && item->song() == song){
			item->songDeleted();
			return;
		}
	}
}

void PlaylistView::slotButtonClear(){
	if(m_playlist)
		m_playlist->clear();
}

/*void PlaylistView::slotPlayerStateChanged(Player::State state){
	if(state == Player::Stopped && m_current != 0){
		m_current->highlight(false);
		m_current = 0;
	}	
}*/

void PlaylistView::slotPlaylistSequential(){
	m_playlist->setProgression(TrackProgression::Default, m_playlist->repeatType());
}
	
void PlaylistView::slotPlaylistRandom(){
	m_playlist->setProgression(TrackProgression::Random, m_playlist->repeatType());
}

void PlaylistView::slotPlaylistNoRepeat(){
	m_playlist->setProgression(m_playlist->progressionType(), TrackProgression::NoRepeat);
}

void PlaylistView::slotPlaylistRepeatAll(){
	m_playlist->setProgression(m_playlist->progressionType(), TrackProgression::RepeatAll);
}

void PlaylistView::slotPlaylistRepeatOne(){
	m_playlist->setProgression(m_playlist->progressionType(), TrackProgression::RepeatOne);
}

void PlaylistView::slotRequestedEditMetadata(QList<QString> songPaths){
	emit requestedEditMetadata(songPaths);
}

void PlaylistView::slotRemoveSongs(QList<short int> indices){
	emit removeSongs(indices);
}

void PlaylistView::addMedia(const QStringList &media){
	m_listWidget->addMedia(media);
}
